package com.net.xpay.openapi.validator;

import com.google.common.base.Strings;
import com.net.xpay.openapi.model.OpenApiChargeRecordChargeModel;
import com.net.common.exception.BusinessException;
import com.net.xpay.common.domain.User;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.xpay.core.domain.ChargeRecord;
import com.net.xpay.core.enums.BillPayMethod;
import com.net.xpay.core.manager.ChargeRecordManager;
import com.net.xpay.core.model.BuyModel;
import com.net.xpay.core.utils.MoneyCheckUtil;
import com.net.xpay.core.utils.UrlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
  on 24/04/2018.
 * 验证PayModel的一些逻辑
 * @see BuyModel
 */
@Component
public class OpenApiChargeRecordValidator {

    @Autowired
    private ChargeRecordManager chargeRecordManager;

    public void ensureChargeParameter(User user, OpenApiChargeRecordChargeModel model) {
        if (model.getBillPayMethod() != BillPayMethod.ALIPAY_WEB_PAGE) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "充值方式不正确");
        }
        if (Strings.isNullOrEmpty(model.getImportBizNo())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL,"外部订单号不能为空");
        }
        ChargeRecord chargeRecord = chargeRecordManager.findByImportBizNo(user.getId(),model.getImportBizNo());
        if (chargeRecord != null){
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL,"外部订单号已被使用");
        }
        if (Strings.isNullOrEmpty(model.getAmount())){
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL,"充值金额不能为空");
        }
        if (!MoneyCheckUtil.isValidMoney(model.getAmount(), 2)){
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL,"充值金额格式错误，最多2位小数");
        }

        if (!Strings.isNullOrEmpty(model.getNotifyUrl()) && model.getNotifyUrl().length() > 255) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "推送地址长度不能超过255个字");
        }

        if (Strings.isNullOrEmpty(model.getReturnUrl())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "回跳地址不能为空");
        }

        if (model.getReturnUrl().length() > 255) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "回跳地址长度不能超过255个字");
        }

        if (!Strings.isNullOrEmpty(model.getNotifyUrl())) {
            if (!UrlUtil.isHttpUrl(model.getNotifyUrl())) {
                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "推送地址格式不正确");
            }
        }
        if (model.getExpireMinutes() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "过期时间不能为空");
        }
        if (model.getExpireMinutes() != null) {
            if (model.getExpireMinutes() < 1L) {
                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "过期时间不能少于1分钟");
            }
            if (model.getExpireMinutes() > 24 * 60L) {
                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "过期时间不能超过1天");
            }
        }
    }


}

